﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Library.Models;
using Library.Repository;
using Library.TwiCommunication;
using Library.Utils;
using log4net;

namespace TWIconnectionTest
{
    class Program
    {
        private static readonly ILog log = LogManager.GetLogger("MainTWI");

        static void Main(string[] args)
        {
            log.Info("start... Linux? " + UserRepository.IsLinux);

            string exit = "";
            //IUserRepository repository = ServiceLocator.Instance.GetService<IUserRepository>();
            //IDeviceRepository deviceRepository = ServiceLocator.Instance.GetService<IDeviceRepository>();

            if(!UserRepository.IsLinux) { log.Error("\n\nDostęp do TWI wymaga linuxa.\n\n");}

            var communicator = ServiceLocator.Instance.GetService<ITwiCommunicator>();
            var deviceRepository = ServiceLocator.Instance.GetService<IDeviceRepository>();

                
            while(exit != "q")
            {
                var devices = deviceRepository.GetAllWithData();

                foreach (var device in devices)
                {
                    Console.WriteLine(device);
                }
                exit = Console.ReadLine();
            }



                //-------------------------------------------------------------------

                //List<DevicePin> pins = new List<DevicePin>();
                //pins.Add(new DevicePin(){Name = "Okno",Pin = 8});
                //InDevice inDevice = new InDevice(){Address = 63 , Description = "blabla", Pins = pins};

                //TermDevice termDevice = new TermDevice() { Address = 79, Description = "Temperaturka"};

                //var communicator = ServiceLocator.Instance.GetService<ITwiCommunicator>();
                //termDevice = communicator.ReadDevice(termDevice);
                //Console.WriteLine(termDevice);


            List<DevicePin> pins2 = new List<DevicePin>();
            pins2.Add(new DevicePin() { Name = "Okno", Pin = 5 });
            pins2.Add(new DevicePin() { Name = "Okno", Pin = 6 });
            OutDevice outDevice = new OutDevice() { Address = 56, Description = "Wyjscie", Pins = pins2 };


                //while(exit != "q")
                //{
                //    var gotDevice = communicator.ReadDevice(termDevice);
                //    Console.WriteLine("Odczytane: {0}", gotDevice);

                //    exit = Console.ReadLine();
                //}

            exit = "";
            while (exit != "q")
            {
                Console.Write("Wpisz wartość pinu 5 (0/1): ");
                if ((exit = Console.ReadLine()) == "0") outDevice.Pins.Find(p => p.Pin == 5).Value = false;
                else outDevice.Pins.Find(p => p.Pin == 5).Value = true;

                communicator.WriteDevice(outDevice);

            }



            



        }
    }
}
